package zl.ordermanager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import zl.ordermanager.service.CustomerService;
import zl.ordermanager.service.DriverService;
import zl.ordermanager.service.PurchaseOrderService;
import zl.ordermanager.service.SalesOrderService;
import zl.ordermanager.service.UserService;
import zl.ordermanager.vo.Customer;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.Driver;
import zl.ordermanager.vo.ECustomerType;
import zl.ordermanager.vo.EInvoiceType;
import zl.ordermanager.vo.ERoles;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.PurchaseOrder;
import zl.ordermanager.vo.SalesOrder;
import zl.ordermanager.vo.Total;
import zl.ordermanager.vo.User;

@Controller
@RequestMapping("/")
public class PageController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private SalesOrderService salesOrderService;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	@Autowired
	private DriverService driverService;

	@Autowired
	private CustomerService customerService;

	@RequestMapping("")
	public String root() {

		return "index";
	}

	@RequestMapping("/index")
	public String index() {

		return "index";
	}

	@RequestMapping("/login")
	public String login() {

		return "login";
	}

	@RequestMapping("/showSalesOrderLookup")
	public String showSalesOrderLookup(PageHelper pageHelper, Model model) {
		DataGrid<SalesOrder> dataGrid = salesOrderService.dataGrid(pageHelper);
		pageHelper.setTargetType("dialog");
		List<SalesOrder> salesOrderList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("salesOrderList", salesOrderList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);

		return "salesOrderLookup";
	}

	@RequestMapping("/showPurchaseOrderLookup")
	public String showPurchaseOrderLookup(PageHelper pageHelper, Model model) {
		Total total = new Total();
		DataGrid<PurchaseOrder> dataGrid = purchaseOrderService.dataGrid(pageHelper, total);
		pageHelper.setTargetType("dialog");
		List<PurchaseOrder> purchaseOrderList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("purchaseOrderList", purchaseOrderList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);
		model.addAttribute("invoiceTypeList", EInvoiceType.values());

		return "purchaseOrderLookup";
	}

	@RequestMapping("/showDriverLookup")
	public String showDriverLookup(PageHelper pageHelper, Model model) {
		DataGrid<Driver> dataGrid = driverService.dataGrid(pageHelper);
		pageHelper.setTargetType("dialog");
		List<Driver> driverList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("driverList", driverList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);

		return "driverLookup";
	}

	@RequestMapping("/showSalesmanSuggest")
	public String showSalesmanSuggest(Model model) {
		List<User> salesmanList = userService.getList(ERoles.SALESMAN);

		model.addAttribute("salesmanList", salesmanList);

		return "salesmanSuggest";
	}

	@RequestMapping("/showDrawerSuggest")
	public String showfinanceSuggest(Model model) {
		List<User> financeList = userService.getList(ERoles.FINANCE);
		List<User> financeAssistList = userService.getList(ERoles.FINANCE_ASSIST);

		if (financeList.isEmpty()) {
			model.addAttribute("financeList", null);
		} else {
			model.addAttribute("financeList", financeList);
		}
		if (financeAssistList.isEmpty()) {
			model.addAttribute("financeAssistList", null);
		} else {
			model.addAttribute("financeAssistList", financeAssistList);
		}

		return "drawerSuggest";
	}

	@RequestMapping("/showTechnicianSuggest")
	public String showTechnicianSuggest(Model model) {
		List<User> technicianList = userService.getList(ERoles.TECHNICIAN);

		model.addAttribute("technicianList", technicianList);

		return "technicianSuggest";
	}

	@RequestMapping("/showDriverSuggest")
	public String showDriverSuggest(Model model) {
		List<Driver> driverList = driverService.dataGrid().getList();

		model.addAttribute("driverList", driverList);

		return "driverSuggest";
	}

	@RequestMapping("/showCustomerLookup")
	public String showCustomerLookup(PageHelper pageHelper, Model model) {
		DataGrid<Customer> dataGrid = customerService.dataGrid(pageHelper);
		pageHelper.setTargetType("dialog");
		List<Customer> customerList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("customerList", customerList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);

		return "customerLookup";
	}

	@RequestMapping("/showCustomerTypeSuggest")
	public String showCustomerTypeSuggest(Model model) {
		model.addAttribute("customerTypeList", ECustomerType.values());

		return "customerTypeSuggest";
	}

}
